# Maintainer: Jeroen Ooms <jeroen@berkeley.edu>

_realname=hdf
pkgbase=mingw-w64-${_realname}4
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}4")
pkgver=4.2.15
pkgrel=7
pkgdesc="Legacy HDF4 driver for GDAL (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url='https://support.hdfgroup.org/products/hdf4/'
license=('GPL2')
source=(#"https://support.hdfgroup.org/ftp/HDF/HDF_Current/src/${_realname}-${pkgver}.tar.gz"
        "https://support.hdfgroup.org/ftp/HDF/releases/HDF${pkgver}/src/hdf-${pkgver}.tar.bz2"
        001-mingw.patch
        002-pkgconfig-without-versioning.patch
        003-17.patch
        004-aarch64.patch)
sha256sums=('bde035ef5a1cd5fdbd0a7f1fa5c17e98bbd599300189ac4d234f16e9bb7bcb12'
            'f842fb5efe99b8ad3e4ecb7683af106c4891d994abb44adf8fd34a54cb729a38'
            '0362725aa964bff6318d37740481e462d77c2d1c39cccc86690773de3d69e94c'
            '6d02e085ec8e53c94894b97400447d3369796e9c0f7c8e23d1da01c4ef2f8371'
            'bd9e7863c33f5912d77950a634d235e4ff3104abac684234719ce37e8065fd01')
depends=("${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran")
         )
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* && ${MSYSTEM} != *64 ]] || echo \
               "${MINGW_PACKAGE_PREFIX}-fc")
             )

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/001-mingw.patch
  patch -p1 -i ${srcdir}/002-pkgconfig-without-versioning.patch

  # https://github.com/HDFGroup/hdf4/pull/17
  patch -p1 -i "${srcdir}/003-17.patch"

  patch -p1 -i ${srcdir}/004-aarch64.patch
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  local -a _enable_fortran
  if [[ ${MINGW_PACKAGE_PREFIX} != *-clang-* || ${MSYSTEM} == *64 ]]; then
    _enable_fortran="ON"
  else
    _enable_fortran="OFF"
  fi

  if [[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]]; then
    export FFLAGS="-fallow-argument-mismatch"
  else
    CFLAGS+=" -Wno-incompatible-function-pointer-types -Wno-implicit-function-declaration"
  fi

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${_extra_config[@]}" \
    -DBUILD_SHARED_LIBS=ON \
    -DHDF4_BUILD_TOOLS=OFF \
    -DHDF4_BUILD_UTILS=ON \
    -DHDF4_BUILD_FORTRAN=${_enable_fortran} \
    -DHDF4_NO_PACKAGES=ON \
    -DHDF4_ENABLE_NETCDF=OFF \
    -DHDF4_INSTALL_CMAKE_DIR="lib/cmake" \
    -DHDF4_INSTALL_DATA_DIR="share/docs/hdf4" \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/hdf4/*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done

  for pcfile in "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pcfile}"
  done
  sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pkgdir}${MINGW_PREFIX}"/lib/libhdf4.settings
}
